<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Recipients extends CI_Controller {


	function __construct(){

		parent::__construct();
		if(!isset($_SESSION)): session_start(); endif;
		(isset($_SESSION['is_logged_in']) && $_SESSION['is_logged_in'] && isset($_SESSION['email']))? '':redirect('home');
		$this->load->model('recipients_model');
		$this->load->model('users_model');

	}



	public function index(){
		$post = $this->input->post();
		$this->page($post);
	}
	function page($post=""){
		$search = '';
		if(!empty($post)){
			$r_search = $post['r-search'];
			switch($post['search-list']){
				case 1: $search =" AND fname LIKE '%$r_search%'";break;//first name
				case 2: $search =" AND lname LIKE '%$r_search%'";break;//last name
				case 3: $search =" AND mname LIKE '%$r_search%'";break;//middlename
				default:$search =" AND (fname LIKE '%$r_search%' OR lname LIKE '%$r_search%' OR mname LIKE '%$r_search%')";break;
			}
		}
		$start_row = $this->uri->segment(3);
		$limit = 7; //result per page
		if(trim($start_row) == ""){
			$start_row = 0;
		}
		$data['recipients_all'] = $this->recipients_model->getRecipients($search);
		$data['recipients'] = $this->recipients_model->getRecipients($search,$start_row,$limit);
		$this->load->library('pagination');
		$config['base_url'] = site_url('recipients/page/');
		$config['total_rows'] = count($data['recipients_all']);
		$config['per_page'] = $limit;
		$config['uri_segment'] = 3;
		$config['anchor_class'] = 'class="button pagi" ';
		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		$data['title']= "Recipients";
		if($search == ''){
			$data['main_content'] = 'profile/recipients_view';
			$this->load->view('include/template', $data);
		}else{
			$output = '';
			$output .= '<div class="pagination">'.$data['pagination'].'</div>';
			$output .= '<div class="clear"></div>';
			$output .= '<div class="notice"></div>';
			$output .= '<input type="hidden" id="path" name="path" value="'.base_url().'"/>';
			foreach ($data['recipients'] as $row){
				$user_id = urlencode($this->encryption_lib->encode($row->recipientid));
				$output .= '<div id = "recipient-context_"'.$user_id.'" class="recipient-context">';
					$output .= '<label>'.$row->fname.'&nbsp;'.$row->lname.'</label>';
					$output .= '<p>'.$row->address.'</p>';
					$output .= '<p>'.$row->phone.'</p>';
					$output .= '<div id = "recipient-control">';
						$output .= '<a href = "javascript:void(0);" onclick="edit_recipient('.$user_id.')">';
						$output .= '<img src = "'.base_url().'"assets/images/edit-btn.png" alt = "" /></a> &nbsp;&nbsp;&nbsp;';
						$output .= '<a href = "javascript:void(0)" onclick="deleteRecipient('.$user_id.')">';
						$output .= '<img src = "'.base_url().'"assets/images/del-btn.png" alt = "" /></a>&nbsp;&nbsp;';
					$output .= '</div>';
				$output .= '</div>';
			}//end foreach
			echo $output;
		}

	}


	function type()
	{
		$data['recipients'] = $this->recipients_model->getRecipients();
		$data['records'] = $this->users_model->get_user();
		$data['title']= "Receptients";
		$data['main_content'] = 'profile/recipients_type_view';


		$this->load->view('include/template', $data);

	}


	function create_recipients(){
		$query = $this->recipients_model->addRecipients();
		echo ($query)? 'true':'false';
		return $query;

	}

	function delete_recipients(){
		$id = $this->input->post('rec_id');
		$rec_id = urldecode($this->encryption_lib->decode($id));
		$delete = $this->recipients_model->delete_recipient($rec_id);
		echo ($delete)? '1':'0';//1 - successfull 0 - unsucessfull
	}
	function edit_recipient(){
		$output = '';
		$id = $this->input->post('rec_id');
		$rec_id = urldecode($this->encryption_lib->decode($id));
		$rec_info = $this->recipients_model->get_recipient_info($rec_id);
		$relationships = $this->recipients_model->get_relationships();
		$output .= '<input type = "hidden" value = "'.$id.'" name = "edit-recep-id" id = "edit-recep-id" />';
		$output .= '<p class = "add-recep"><input type = "text" value = "'.$rec_info['fname'].'" name = "add-recep-fname" id = "add-recep-fname" /></p>';
		$output .= '<p class = "add-recep"><input type = "text" value = "'.$rec_info['mname'].'" name = "add-recep-mname" id = "add-recep-mname" /></p>';
		$output .= '<p class = "add-recep"><input type = "text" value = "'.$rec_info['lname'].'" name = "add-recep-lname" id = "add-recep-lname" /></p>';
		$output .= '<p class = "add-recep">';
			$output .= '<input type = "text" value = "'.$rec_info['address'].'" name = "add-recep-address" id = "add-recep-address" style="margin-bottom:0px;"/>';
			$output .= '<div id="address_div"><span class="add_city">City</span> <span class="add_state">State</span><span class="add_zip">Zip</span></div>';
			$output .= '<div class="clear"></div>';
		$output .= '</p>';
		$output .= '<p class = "add-recep">';
			$output .= '<label>Relationship</label>';
			$output .= '<select name="add-recep-rel">';
				foreach($relationships as $rel):
					$selected = ($rec_info['relationship'] == $rel['id'])? 'selected="selected"':'';
					$output .= '<option value="'.$rel['id'].'"'.$selected.'>'.$rel['title'].'</option>';
				endforeach;
			$output .= '</select>';
		$output .= '</p>';
		$output .= '<p class = "add-recep"><input type = "text" value = "'.$rec_info['phone'].'" name = "add-recep-phone" id = "add-recep-phone" /></p>';
		$output .= '<p class = "add-recep"><input type = "text" value = "'.$rec_info['email'].'" name = "add-recep-email" id = "add-recep-email" /></p>';
		echo $output;
	}
	function update_recipient(){
		$post = $this->input->post();
		$update = $this->recipients_model->update_recipient($post);
		echo ($update)? '1':'0';
	}
	function check_email_exist(){
		$email = $this->input->post('email');
		$exist = $this->recipients_model->check_email_recipient_exist($email);
		echo ($exist)? '1':'0';
	}
	function add_recipients(){
		$data['recipients'] = $this->recipients_model->getRecipients();
		$data['relationships'] = $this->recipients_model->get_relationships();
		$data['records'] = $this->users_model->get_user();
		$data['title']= "My Life My Eulogy | Add Recipients";
		$data['main_content'] = 'profile/add_recipients_type_view';
		$this->load->view('include/template', $data);
	}

}// end of controller
